import 'package:flutter/material.dart';
import 'package:imitating_tiktok/pages/home_pages/models/video_model.dart';
import 'package:video_player/video_player.dart';

class HomeVideoDetail extends StatefulWidget {
  final Video video;
  final int snapPlayIndex;
  final int currentPlayIndex;
  const HomeVideoDetail(
      {Key? key,
      required this.video,
      required this.snapPlayIndex,
      required this.currentPlayIndex})
      : super(key: key);

  @override
  _HomeVideoDetailState createState() => _HomeVideoDetailState();
}

class _HomeVideoDetailState extends State<HomeVideoDetail> {
  late final VideoPlayerController _videoPlayerController;
  late Future _initializeVideoPlayer;

  @override
  void initState() {
    _videoPlayerController =
        VideoPlayerController.asset(widget.video.videoName);
    _initializeVideoPlayer = _videoPlayerController.initialize();
    _videoPlayerController.setLooping(true);
    // _videoPlayerController.play();

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    (widget.snapPlayIndex == widget.currentPlayIndex)
        ? _videoPlayerController.play()
        : _videoPlayerController.pause();

    return Container(
      child: FutureBuilder(
          future: _initializeVideoPlayer,
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.done) {
              return VideoPlayer(_videoPlayerController);
            } else {
              return Container(
                color: Colors.deepPurpleAccent,
              );
            }
          }),
    );
  }
}
